package com.ewzj.bigdata.mvp.ui.activity

import android.animation.IntEvaluator
import android.animation.ValueAnimator
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.inputmethod.EditorInfo
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.extensions.dpToPx
import com.ewzj.bigdata.di.component.DaggerSearchActivityComponent
import com.ewzj.bigdata.di.module.SearchActivityModule
import com.ewzj.bigdata.mvp.contract.SearchActivityContract
import com.ewzj.bigdata.mvp.presenter.SearchActivityPresenter
import com.ewzj.bigdata.mvp.ui.adapter.HotkeyAdapter
import com.ewzj.bigdata.mvp.ui.adapter.TypeAdapter
import com.jakewharton.rxbinding2.view.RxView
import com.jakewharton.rxbinding2.widget.RxTextView
import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.DeviceUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_search.*

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class SearchActivity :BaseActivity<SearchActivityPresenter>(),SearchActivityContract.View{
    override fun setSearchetString(keyword: String) {
        et_searchcontent.setText(keyword)
    }
    override fun switchAdapter(adapter: BaseQuickAdapter<Any, BaseViewHolder>, keyword:String) {
        tv_title.text=housetype+","+keyword
        rv_search.adapter=adapter
    }

    var housetype="二手房"
    override fun inittypeAdapter(adapter: TypeAdapter) {
       rv_content.layoutManager=GridLayoutManager(this,5) as LinearLayoutManager
       rv_content.adapter=adapter
       adapter.listener=object : TypeAdapter.TypechageListener {
           override fun typeChange(name: String) {
               housetype = name
               tv_type.setText(name)
               showSerachBar(90.dpToPx(this@SearchActivity), 0)
               mPresenter.searchData(et_searchcontent.text.toString(),housetype)
           }

       }
    }

    override fun initAdapter(adapter: HotkeyAdapter) {
        rv_search.layoutManager=LinearLayoutManager(this)
        val emptyview = LayoutInflater.from(this).inflate(R.layout.empty_view, null, false)
        val footerview = LayoutInflater.from(this).inflate(R.layout.footer_view, null, false)
        adapter.emptyView=emptyview
        adapter.addFooterView(footerview)
        RxView.clicks(footerview).subscribe( { mPresenter.clearHis() })
        rv_search.adapter=adapter
        adapter.notifyDataSetChanged()
    }

    override fun initData(savedInstanceState: Bundle?) {
        initListener()
        mPresenter.initHomeAdapter(rv_content)
        mPresenter.inittype()
        mPresenter.loadHisData()
    }


    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_search
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        DaggerSearchActivityComponent.builder().appComponent(appComponent)
                .searchActivityModule(SearchActivityModule(this))
                .build()
                .inject(this)
    }

    override fun showLoading() {

    }

    override fun launchActivity(intent: Intent?) {

    }

    override fun hideLoading() {

    }

    override fun killMyself() {
    }

    override fun showMessage(message: String?) {
    }

    fun showSerachBar(startValue: Int, endValue: Int) {
        //      RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) rlSearch.getLayoutParams();
        val valueAnimator = ValueAnimator.ofInt(0, 100)
        valueAnimator.duration = 200
        valueAnimator.addUpdateListener { animation ->
            val animatorValue = animation.animatedValue as Int
            val fraction = animatorValue / 100f
            val mEvaluator = IntEvaluator()
            //3.使用IntEvaluator计算属性值并赋值给ListView的高
            val layoutParams = ll_content.layoutParams as LinearLayout.LayoutParams
            layoutParams.height = mEvaluator.evaluate(fraction, startValue, endValue)!!
            ll_content.layoutParams = layoutParams
            ll_content.requestLayout()
        }
        valueAnimator.setTarget(ll_content)
        valueAnimator.start()
    }
    private fun initListener() {
        RxView.clicks(ll_type).subscribe({if(ll_content.height==0){
            showSerachBar(0, 90.dpToPx(this))
        }else{
            showSerachBar(90.dpToPx(this),0)
        }})
        RxTextView.editorActions(et_searchcontent).observeOn(AndroidSchedulers.mainThread()).subscribe(Consumer { t ->
            run {
                if (t == EditorInfo.IME_ACTION_SEARCH) {
                    mPresenter.searchData(et_searchcontent.text.toString(),housetype)
                    DeviceUtils.hideSoftKeyboard(this@SearchActivity,et_searchcontent)
                }
            }
        })
        RxView.clicks(iv_search).subscribe({ mPresenter.searchData(et_searchcontent.text.toString(),housetype)
            DeviceUtils.hideSoftKeyboard(this@SearchActivity,et_searchcontent)})
        RxView.clicks(tv_cancel).subscribe({finish()})
    }

    override fun onPause() {
        mPresenter.saveData()
        super.onPause()
    }

}